package news.paper.tintuc.tinnong.docbao.doctin.fragment;

import news.paper.tintuc.tinnong.docbao.doctin.ActivityNewsDetail;
import news.paper.tintuc.tinnong.docbao.doctin.ActivitySettings;
import news.paper.tintuc.tinnong.docbao.doctin.BaseActivity_Menu;
import news.paper.tintuc.tinnong.docbao.doctin.R;
import news.paper.tintuc.tinnong.docbao.doctin.GCM.PopupAD;
import news.paper.tintuc.tinnong.docbao.doctin.adapter.AdapterNews;
import news.paper.tintuc.tinnong.docbao.doctin.controller.InterfaceAdapter;
import news.paper.tintuc.tinnong.docbao.doctin.model.News;
import news.paper.tintuc.tinnong.docbao.doctin.util.Constrant;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.startapp.android.publish.banner.Banner;

@SuppressLint("ValidFragment")
public class Fragment_ListNews extends Fragment {

	public BaseActivity_Menu sfContext;
	private PullToRefreshListView listView;
	// int page_total_new, page_current_new;
	private AdapterNews adapter_new;
	private View view_load_more;
	private TextView title;

	public Fragment_ListNews() {
		super();
	}

	public Fragment_ListNews(BaseActivity_Menu parentContext) {
		super();
		sfContext = parentContext;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (adapter_new != null)
			adapter_new.notifyDataSetChanged();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LinearLayout ll = (LinearLayout) inflater.inflate(
				R.layout.layout_listview, null);
		Constrant.adapter.setNotifyOnChange(true);
		// ////////////////////////////////////////////////////////
		view_load_more = (LinearLayout) ll
				.findViewById(R.id.layout_load_more_linear);
		view_load_more.setVisibility(View.GONE);
		listView = (PullToRefreshListView) ll.findViewById(R.id.list);
		adapter_new = new AdapterNews(sfContext, new InterfaceAdapter() {

			@Override
			public void start_load() {
				view_load_more.setVisibility(View.VISIBLE);
			}

			@Override
			public void finish_load() {
				view_load_more.setVisibility(View.GONE);
				listView.onRefreshComplete();
			}
		});
		listView.setAdapter(adapter_new);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(sfContext, ActivityNewsDetail.class);
				News news = (News) adapter_new.getItem(position - 1);
				intent.putExtra("NEWS", news);
				sfContext.startActivity(intent);
			}
		});
		adapter_new.load_data(Constrant.SUBID);
		// //////////////////////////
		title = (TextView) ll.findViewById(R.id.title_text);
		title.setText("Tin nóng");
		ImageView imageView = (ImageView) ll
				.findViewById(R.id.title_button_menu);
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sfContext.toggle();
			}
		});

		ImageView imageView_setting = (ImageView) ll
				.findViewById(R.id.title_button_menu_right);
		imageView_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(sfContext, ActivitySettings.class);
				sfContext.startActivity(intent);
			}
		});
		listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				String label = DateUtils.formatDateTime(sfContext,
						System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
								| DateUtils.FORMAT_SHOW_DATE
								| DateUtils.FORMAT_ABBREV_ALL);
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				adapter_new.re_int();
			}
		});
		if (PopupAD.ShowAd == 1) {
		}else{
			Banner banner = (Banner) ll.findViewById(R.id.startAppBanner1);
			banner.hideBanner();
		}
		return ll;
	}

	public void re_int(int index) {
		adapter_new.re_int(index);
		title.setText(Constrant.arrayListNews.get(index).getListName());
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
}